<?php
	/**
	* Bill Manager
	* return in Json format
	* @author trungdt
	* @since Oct 22, 2011
	*/
	if (!defined('EXPONENT')) exit('');

	$data=array();

	$bill_id="";
	if (isset($_REQUEST['bill_id']))
		$bill_id=intval($_REQUEST['bill_id']);

	$have_permission=false;
	if (exponent_users_isAdmin() || exponent_permissions_check(array('administrate', "bill_management"),$loc))
		$have_permission=true;
	$currency=productsmodule::_GetCurrencyDefault(LANG, 1);

	if (exponent_users_isLoggedIn())
	{    
		if ($bill_id == "")
		{
			global $user;
			$sql_where="";
			if (!$have_permission)
				$sql_where="buyer_user_id = {$user->id}";
			else
				$sql_where=" 1 ";
			
			if (isset($_REQUEST['search_id']))
			{
				$search_id=intval($_REQUEST['search_id']);
				$sql_where.=" AND  (id = {$search_id})";
			}
			
			if (isset($_REQUEST['name']))
			{
				$name=_ab_safe_html_string($_REQUEST['name']);
				$sql_where.=" AND  (buyer_user_name LIKE '{$name}')";
			}
			
			if (isset($_REQUEST['info']))
			{
				$info="%"._ab_safe_html_string($_REQUEST['info'])."%";
				$sql_where.=" AND  (
					(info1_name LIKE '{$info}') OR (info1_address LIKE '{$info}') OR
					(info1_province LIKE '{$info}') OR (info1_city LIKE '{$info}') OR
					(info1_phone LIKE '{$info}') OR (info1_note LIKE '{$info}') OR
					(info1_phone LIKE '{$info}') OR (info1_note LIKE '{$info}') OR
					
					(info2_name LIKE '{$info}') OR (info2_address LIKE '{$info}') OR
					(info2_province LIKE '{$info}') OR (info2_city LIKE '{$info}') OR
					(info2_phone LIKE '{$info}') OR (info2_note LIKE '{$info}') OR
					(info2_phone LIKE '{$info}') OR (info2_note LIKE '{$info}')
				)";
			}
			
			// date
			if (isset($_REQUEST['mdate']))
			{
				$mdate=_ab_safe_html_string($_REQUEST['mdate']);
				$b_date=strtotime($mdate);
				$e_date=$b_date + 24 * 3600-1;
				$sql_where.=" AND  (buy_date>= {$b_date}) AND (buy_date <= {$e_date})";
			}
			
			if (isset($_REQUEST['bill_state']))
			{
				$bill_state=intval($_REQUEST['bill_state']);
				$sql_where.=" AND  ( bill_state = {$bill_state})";
			}
			
			// page, rows, sort
			$page=1;
			$rows=20;
			$sort="";
			if (isset($_REQUEST['page']))
				$page = intval($_REQUEST['page']);
			if (isset($_REQUEST['rows']))
				$rows = intval($_REQUEST['rows']);
			if (isset($_REQUEST['sort']))
			{
				$sort = _ab_safe_html_string($_REQUEST['sort']);
				if ($sort == "buy_date_text")
					$sort="buy_date";
				$sort.=" "._ab_safe_html_string($_REQUEST['order']);
			}
			else
				$sort="buy_date DESC, bill_state ASC";
			$start=($page-1)*$rows;
			
				
			// get all bill 
			$data=$db->selectObjects("productsmodule_bills", "{$sql_where} ORDER BY {$sort} LIMIT {$start}, {$rows}");

			foreach ($data as $k=>$v)
			{
				$v->buy_date_text=date("j/n/Y H:i:s",$v->buy_date);
				$v->bill_total = productsmodule::_GetCurrencyFormat($currency, $v->bill_total);
				$v->included_tax = productsmodule::_GetCurrencyFormat($currency, $v->included_tax);
				$v->included_shipping = productsmodule::_GetCurrencyFormat($currency, $v->included_shipping);
			}
			$ret=null;
			$ret->total=count($data);
			$ret->rows=$data;
			echo json_encode($ret);
		}
		else
		{
			// get bill detail
			$sql_where="(id = {$bill_id})";
			if (!$have_permission)
				$sql_where.=" AND (buyer_user_id = {$user->id})";
			$bill=$db->selectObject("productsmodule_bills", $sql_where);
			
			$bill->bill_total = productsmodule::_GetCurrencyFormat($currency, $bill->bill_total);
			$bill->included_tax = productsmodule::_GetCurrencyFormat($currency, $bill->included_tax);
			$bill->included_shipping = productsmodule::_GetCurrencyFormat($currency, $bill->included_shipping);
			
			
			//productsmodule::__Bill_AddLogs("User {$user->firstname} {$user->lastname}({$user->username}) accessed this bill", $bill_id);
			if (isset($bill->id))
			{
				// get logs
				$logs=$db->selectObjects("productsmodule_bills_logs", "bill_id = {$bill_id} ORDER BY post_date DESC, id  DESC");

				foreach ($logs as $k=>$v)
				{
					$v->post_date = date("j/n/Y H:i:s",$v->post_date);
				}

				// get each product detail
				$products=$db->selectObjects("productsmodule_bills_details", " bill_id = {$bill_id}");
				
				foreach ($products as $k=>$v)
				{
					// format currency
					$price_save=$v->product_price_orginal - $v->product_price;
					$v->price_save_percent=round((floatval($price_save)/$v->product_price_orginal)*100,2);
					$v->product_price = productsmodule::_GetCurrencyFormat($currency, $v->product_price);
					$v->product_price_orginal = productsmodule::_GetCurrencyFormat($currency, $v->product_price_orginal);

				}
				
				$print=0;
				if (isset($_REQUEST['print']))
					$print=1;

				// fetch to template
				$template=new template("productsmodule","_Bill_Detail_in_Management",$loc);
				$template->register_permissions(array('administrate',"bill_management"),$loc);
				$template->assign("have_permission", $have_permission?1:0);
				$template->assign("products", $products);
				$template->assign("bill", $bill);
				$template->assign("logs", $logs);
				$template->assign("print", $print);
				$template->output();
			}
		}
		exponent_theme_setFlagAjaxAction(true);
	}
	else
		die();
?>